package com.sky.financelake.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.sky.financelake.R;
import com.sky.financelake.adapter.SimplePlateAdapter;
import com.sky.financelake.base.BaseToolA;
import com.sky.financelake.bean.dto.SimpleStockDto;
import com.sky.financelake.databinding.ActivityPlateToolBinding;
import com.sky.financelake.databinding.CommonNoDataBinding;
import com.sky.financelake.viewmodels.remote.StockViewModel;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class PlateToolA extends BaseToolA {

    private ActivityPlateToolBinding binding;
    private SimplePlateAdapter adapter;
    private List<SimpleStockDto> stockDtoList = new ArrayList<>();
    private StockViewModel mViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPlateToolBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Toolbar toolbar = binding.toolbarContainer.toolbar;
        initToolbar(toolbar, getString(R.string.plate_tool));

        mViewModel = new ViewModelProvider(this).get(StockViewModel.class);

        AppCompatEditText etPlate = binding.etPlate;

        CommonNoDataBinding noData = binding.containerNoData;

        AppCompatTextView tvSearch = binding.tvSearch;
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = etPlate.getText().toString();
                if (key.length() == 0) {
                    Snackbar.make(etPlate, "未输入板块名称", Snackbar.LENGTH_SHORT).show();
                } else {
                    mViewModel.getRelativeStockByPlates(key);
                }
            }
        });

        RecyclerView rvStock = binding.rvStock;
        rvStock.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SimplePlateAdapter(stockDtoList, this);
        rvStock.setAdapter(adapter);
        mViewModel.getSimpleStockListLiveData().observe(this, response -> {
            stockDtoList.clear();
            List<SimpleStockDto> tmp = response.getResult();
            if (tmp.size() == 0) {
                noData.getRoot().setVisibility(View.VISIBLE);
                rvStock.setVisibility(View.GONE);
            } else {
                noData.getRoot().setVisibility(View.GONE);
                rvStock.setVisibility(View.VISIBLE);
                stockDtoList.addAll(tmp);
                adapter.notifyDataSetChanged();
            }

        });

    }


}
